package proyecto2a;

import java.awt.Point;
import java.util.Random;
import java.util.Vector;

import javax.swing.ImageIcon;

public class MenteMaestra extends Mente {

	protected Vector<Nave> naves;
	protected int flota = 11;
	
	public MenteMaestra() {
		super();
		this.naves = new Vector<Nave>();
	}
	
	public Vector<Nave> getNaves()
	{
		return this.naves;
	}
	
	
	@Override
	public void preparar() {
		int maxWidth = this.juego.getWidth()-40;
		int maxHeight = this.juego.getHeight()-50;
		for( int i = 0; i < this.flota; i++ ) {
			Nave nave = new Nave(new ImageIcon(getClass().getResource("/images/HAASLICH.gif")));
			int posX = new Random(System.nanoTime()).nextInt(maxWidth);
			int posY = new Random(System.nanoTime()).nextInt(maxHeight);		
			nave.setLocation(posX, (-1*posY)-50);
			nave.setSize(28,28);
			this.juego.agregarNave(nave);
			this.naves.add(nave);
		}

	}

	@Override
	public void jugar() {
		this.start();
	}
	
	public void run() {
		int maxHeight = this.juego.getHeight();
		while(naves.size()!=0) {
			synchronized(naves){
				for(Nave p:naves) {
					Point pos = p.getLocation();
					Point nuevaPos;
					if (pos.y < maxHeight ) {
						nuevaPos = new Point(pos.x, pos.y+6);
					} else {
						int posY = new Random(System.nanoTime()).nextInt(maxHeight);	
						nuevaPos = new Point(pos.x, (-1*posY)-50);
					}
					p.setLocation(nuevaPos);
				}
			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		if(naves.size()==0)
			juego.setWinner();
		this.stop();
	}
	

}
